#include <stdio.h>
#include "doorbell_codec.h"
#include "doorbell_led.h"
#include "doorbell_button.h"
#include "doorbell_camera.h"
#include "doorbell_wifi.h"
#include "doorbell_mqtt.h"
#include "doorbell_wsclient.h"
#include <esp_log.h>

#include "doorbell_ota.h"

static RingbufHandle_t mic_buf = NULL;
static RingbufHandle_t speaker_buf = NULL;

static void doorbell_button_cb1(void *arg)
{
    doorbell_led_on();
}

static void doorbell_button_cb2(void *arg)
{
    doorbell_led_off();
}

static void doorbell_wifi_cb(void)
{
    ESP_LOGI("main", "WiFi connected");
    doorbell_mqtt_init();
    doorbell_wsclient_init(mic_buf, speaker_buf);
    doorbell_mqtt_cmd_cb on_cb = {
        .cmd = "on",
        .cb = doorbell_button_cb1,
        .arg = NULL,
    };
    doorbell_mqtt_cmd_cb off_cb = {
        .cmd = "off",
        .cb = doorbell_button_cb2,
        .arg = NULL,
    };
    doorbell_mqtt_register_callback(&on_cb);
    doorbell_mqtt_register_callback(&off_cb);
    doorbell_ota_init();

    doorbell_ota_confirm();
}

void app_main(void)
{
    mic_buf = xRingbufferCreate(65536, RINGBUF_TYPE_NOSPLIT);
    speaker_buf = xRingbufferCreate(65536, RINGBUF_TYPE_NOSPLIT);
    doorbell_codec_init(mic_buf, speaker_buf);
    doorbell_camera_init();
    doorbell_codec_start();

    doorbell_led_init();
    doorbell_button_init();

    doorbell_wifi_init(doorbell_wifi_cb);
}